package com.army.stock.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.army.stock.domain.ArmyDepartment;
import com.army.stock.domain.ArmyGood;
import com.army.stock.domain.ArmyYearplan;
import com.army.stock.domain.ArmyYearplanItem;
import com.army.stock.service.ArmyDepartmentService;
import com.army.stock.service.ArmyGoodService;
import com.army.stock.service.ArmyYearplanService;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.web.url.BaseUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 15/1/5.
 * 年度计划
 */
@Controller
public class ArmyYearPlanController {

    private final static Logger logger = LoggerFactory.getLogger(ArmyYearPlanController.class);

    @Autowired
    private ArmyYearplanService armyYearplanService;

    @Autowired
    private ArmyDepartmentService armyDepartmentService;

    @Autowired
    private ArmyGoodService armyGoodService;

    private final static String YEAR_PLANS = "yearplan/yearplans";

    private final static String NEW_PAGE = "yearplan/new";

    private final static String CHOOSE_GOOD = "yearplan/choosegood";

    private final static String YEAR_PLAN_DETAIL = "yearplan/detail";

    private final static String YEAR_PLAN_DETAIL_ITEM = "yearplan/itemdetail";

    private final static String YEAR_PLAN_DETAIL_ITEM_UPDATE = "yearplan/itemdetailForUpdate";

    private final static String YEAR_PLAN_UPDATE = "yearplan/update";

    @RequestMapping(value = "/yearplans", method = RequestMethod.GET)
    public String yearplans(HttpServletRequest request, ModelMap modelMap) {
        logger.info("进入年度计划页面....");
        BaseUrl baseUrl = new BaseUrl(request);
        String departmentId = "1";
        Map<String, Object> param = baseUrl.getQueryMap();
        param.put("departmentId", departmentId);
        ArmyDepartment armyDepartment = armyDepartmentService.getArmyDepartmentById(Long.valueOf(departmentId));
        PaginatedArrayList<ArmyGood> pageInfo = armyYearplanService.getArmyYearplanPageInfo(
                baseUrl.getCurrentPage(), 10, param);
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        modelMap.put("armyDepartment", armyDepartment);
        return YEAR_PLANS;
    }

    @RequestMapping(value = "/yearplans/new", method = RequestMethod.GET)
    public String addYearplansPage(HttpServletRequest request, ModelMap modelMap) {
        logger.info("进入添加年度计划页面!!!!");
        BaseUrl baseUrl = new BaseUrl(request);
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int nextYear = currentYear + 1;
        String departmentId = "1";
        ArmyDepartment armyDepartment = armyDepartmentService.getArmyDepartmentById(Long.valueOf(departmentId));
        modelMap.put("currentYear", currentYear);
        modelMap.put("nextYear", nextYear);
        modelMap.put("armyDepartment", armyDepartment);
        modelMap.put("baseUrl", baseUrl);
        return NEW_PAGE;
    }

    @RequestMapping(value = "/yearplans/choosegood", method = RequestMethod.GET)
    public String choosegoodPage(HttpServletRequest request, ModelMap modelMap) {
        logger.info("选择商品相关!!!!");
        BaseUrl baseUrl = new BaseUrl(request);
        PaginatedArrayList<ArmyGood> pageInfo = armyGoodService.getArmyGoodPageInfo(
                baseUrl.getCurrentPage(), 10, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return CHOOSE_GOOD;
    }

    @RequestMapping(value = "/yearplans/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(HttpServletRequest request, ModelMap modelMap) {
        String yearPlanData = request.getParameter("yearPlanData");
        logger.info("yearPlanData:{}", yearPlanData);
        Map<String, String> returnMap = new HashMap<String, String>();
        try {
            JSONObject jsonObject = JSON.parseObject(yearPlanData);
            armyYearplanService.addArmyYearPlan(jsonObject);
            returnMap.put("code", "0");
        } catch (Exception e) {
            returnMap.put("code", "-1");
            logger.error("addYearPlan error:", e);
        }
        return JSON.toJSONString(returnMap);
    }

    //年度计划详情
    @RequestMapping(value = "/yearplans/detail", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        String id = request.getParameter("id");
        logger.info("查看年度计划详情:{}", id);
        ArmyYearplan armyYearplan = armyYearplanService.getArmyYearplanById(Long.valueOf(id));
        if (armyYearplan != null) {
            ArmyDepartment armyDepartment = armyDepartmentService.getArmyDepartmentById(armyYearplan.getDepartmentId());
            modelMap.put("armyDepartment", armyDepartment);
            modelMap.put("armyYearplan", armyYearplan);
        }
        return YEAR_PLAN_DETAIL;
    }

    @RequestMapping(value = "/yearplans/ajaxItems", method = RequestMethod.GET)
    public String ajaxItems(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        String id = request.getParameter("id");
        PaginatedArrayList<ArmyYearplanItem> pageInfo = armyYearplanService.getArmyYearplanItemsPageInfo(baseUrl.getCurrentPage(), 100, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return YEAR_PLAN_DETAIL_ITEM;
    }

    @RequestMapping(value = "/yearplans/ajaxItemsForUpdate", method = RequestMethod.GET)
    public String ajaxItemsForUpdate(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        String id = request.getParameter("id");
        PaginatedArrayList<ArmyYearplanItem> pageInfo = armyYearplanService.getArmyYearplanItemsPageInfo(baseUrl.getCurrentPage(), 100, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return YEAR_PLAN_DETAIL_ITEM_UPDATE;
    }

    @RequestMapping(value = "/yearplans/{yearPlanId}/update", method = RequestMethod.GET)
    public String editPage(@PathVariable(value = "yearPlanId") String yearPlanId, HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        modelMap.put("yearPlanId", yearPlanId);
        ArmyYearplan armyYearplan = armyYearplanService.getArmyYearplanById(Long.valueOf(yearPlanId));
        if (armyYearplan != null) {
            ArmyDepartment armyDepartment = armyDepartmentService.getArmyDepartmentById(armyYearplan.getDepartmentId());
            List<Map> armyYearPlanItems = armyYearplanService.getAllArmyYearPlanItems(Long.valueOf(yearPlanId));
            modelMap.put("armyYearPlanItems", armyYearPlanItems);
            modelMap.put("armyDepartment", armyDepartment);
            modelMap.put("armyYearplan", armyYearplan);
            modelMap.put("baseUrl", baseUrl);
        }
        return YEAR_PLAN_UPDATE;
    }

    @RequestMapping(value = "/yearplans/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(HttpServletRequest request, ModelMap modelMap) {
        String yearPlanData = request.getParameter("yearPlanData");
        logger.info("yearPlanData:{}", yearPlanData);
        Map<String, String> returnMap = new HashMap<String, String>();
        try {
            JSONObject jsonObject = JSON.parseObject(yearPlanData);
            armyYearplanService.updateArmyYearPlan(jsonObject);
            returnMap.put("code", "0");
        } catch (Exception e) {
            returnMap.put("code", "-1");
            logger.error("addYearPlan error:", e);
        }
        return JSON.toJSONString(returnMap);
    }

    @RequestMapping(value = "/yearplans/apply", method = RequestMethod.POST)
    @ResponseBody
    public String apply(HttpServletRequest request, ModelMap modelMap) {
        String yearplanid = request.getParameter("yearplanid");
        logger.info("yearplanid:{}", yearplanid);
        Map<String, String> returnMap = new HashMap<String, String>();
        try {
            armyYearplanService.applyArmyYearPlan(Long.valueOf(yearplanid));
            returnMap.put("code", "0");
        } catch (Exception e) {
            returnMap.put("code", "-1");
            logger.error("apply error:", e);
        }
        return JSON.toJSONString(returnMap);
    }

}
